package com.secneo.appshare.adapter;

import java.io.ByteArrayOutputStream;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.secneo.appshare.R;
import com.secneo.appshare.activities.ApplicationActivity;
import com.secneo.appshare.activities.AppsOtherUserActivity;
import com.secneo.appshare.activities.LoginChooseMethodActivity;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.LatestActivity;
import com.secneo.appshare.common.AppSharePreference;

public class ActivityAppDetailAdapter extends BaseAdapter {
	private List<LatestActivity> mList;
	private Context mContext;
	private boolean mStoped;
	private int mCount;
	
	public ActivityAppDetailAdapter(Context ctx, List<LatestActivity> list) 
	{
		this.mContext = ctx;
		this.mList = list;
		this.mStoped = false;
		this.mCount = mList.size();
	}
	
	public void appandAdapter(List<LatestActivity> list) 
	{
		for(int i=0; i<list.size(); i++)
		{
			mList.add(list.get(i));
			mCount++;
		}
	}
	
	public void freeDrawable()
	{
		mStoped = true;
		Log.d("MyTag", "Logs bitmaps free !!! ");
		for(int i=0; i<mList.size(); i++)
		{
			BitmapDrawable a = (BitmapDrawable) mList.get(i).app_icon_drawable;
			if(a!=null && !a.getBitmap().isRecycled())
				a.getBitmap().recycle();
			mList.get(i).app_icon_drawable = null;

			if(!mList.get(i).user_icon.contains("buildin"))
			{
				a = (BitmapDrawable) mList.get(i).user_icon_drawable;
				if(a!=null && !a.getBitmap().isRecycled())
					a.getBitmap().recycle();
				mList.get(i).user_icon_drawable = null;
			}
		}
		System.gc();
	}
	
	@Override
	public int getCount() {
		return mCount;
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		if(mStoped)
			return convertView;
		ActivityItem activityItem;
		final LatestActivity activity = mList.get(position);
		if (convertView == null) 
		{
			View v = LayoutInflater.from(mContext).inflate(R.layout.app_detail_item, null);
			
			activityItem = new ActivityItem();
			activityItem.mUserIcon = (ImageView) v.findViewById(R.id.activity_user_icon);
			activityItem.weiboIcon = (ImageView) v.findViewById(R.id.weibo_Icon);
			activityItem.mUserName = (TextView) v.findViewById(R.id.activity_user_name);
			activityItem.mActivityDetails = (TextView) v.findViewById(R.id.activity_details);
//			activityItem.mActivityDate = (TextView) v.findViewById(R.id.activity_date);
			activityItem.mActivityDate = null;
			activityItem.mAppIcon = (ImageView) v.findViewById(R.id.activity_app_icon);
			v.setTag(activityItem);
			convertView = v;
			
		} 
		else 
		{
			activityItem = (ActivityItem)convertView.getTag();
		}
		
		if (activity.app_icon_drawable == null)
			activityItem.mAppIcon.setImageResource(R.drawable.some_app);
		else 
			activityItem.mAppIcon.setImageDrawable(activity.app_icon_drawable);
		
		if (activity.user_icon_drawable == null) 
			activityItem.mUserIcon.setImageResource(R.drawable.some_user);
		else 
			activityItem.mUserIcon.setImageDrawable(activity.user_icon_drawable);

		if(activity.sns_type != 0)
		{
			int iconResource = LocalStore.getLocaleStoreInstance().getSmallIconResource(activity.sns_type);
			if(iconResource!=0){
				activityItem.weiboIcon.setImageResource(iconResource);
				activityItem.weiboIcon.setVisibility(View.VISIBLE);
			}
		}
		else
		{
			activityItem.weiboIcon.setVisibility(View.INVISIBLE);
		}
		
		activityItem.mAppIcon.setVisibility(View.VISIBLE); // Here is a Shit!	
		if(activity.action_type == 0)
		{
			activityItem.mActivityDetails.setText(mContext.getString(R.string.activity_action0)+" "+activity.user_name);
			activityItem.mAppIcon.setImageResource(R.drawable.newbie);
		}
		else if(activity.action_type == 1)
			activityItem.mActivityDetails.setText(activity.user_name+mContext.getString(R.string.activity_action1)+activity.app_name);
		else if(activity.action_type == 2)
			activityItem.mActivityDetails.setText(activity.user_name+mContext.getString(R.string.activity_action2)+activity.app_name);
		else if(activity.action_type == 3)
			activityItem.mActivityDetails.setText(activity.user_name+mContext.getString(R.string.activity_action3)+activity.app_name+":"+activity.comments);
		else if(activity.action_type == 4)
			activityItem.mActivityDetails.setText(activity.user_name+mContext.getString(R.string.activity_action4)+activity.app_name);
		else if(activity.action_type == 5)
		{
			activityItem.mActivityDetails.setText(activity.user_name+mContext.getString(R.string.activity_action5)+activity.app_name);
			if(activity.app_name.equals("下载勋章"))
				activityItem.mAppIcon.setImageResource(R.drawable.medal_down);
			else
				activityItem.mAppIcon.setImageResource(R.drawable.medal_share);
		}
//		activityItem.mActivityDate.setText(activity.date);
		
		if(activity.user_id.equals(""))
		{
			activityItem.mUserName.setText("游客");
			activityItem.mUserIcon.setOnClickListener(null);
		}
		else
		{
			activityItem.mUserName.setText(activity.user_name);
			activityItem.mUserIcon.setOnClickListener(new OnClickListener()
			{		
				@Override
				public void onClick(View v)
				{
					if(!AppSharePreference.isAppShareUserLogined(mContext))
					{
						Intent intent=new Intent();
						intent.setClass(mContext, LoginChooseMethodActivity.class);
						mContext.startActivity(intent);
					}else
					{
						try 
						{
							Intent intent = new Intent();
							intent.setClass(mContext, AppsOtherUserActivity.class);
		
							Bundle mBundle = new Bundle();
							String uid = LocalStore.getLocaleStoreInstance(mContext).getUid();
							mBundle.putString("uid", uid);
							mBundle.putString("other_uid", activity.user_id);
							mBundle.putString("user_icon", activity.user_icon);
							mBundle.putString("user_name", activity.user_name);
							intent.putExtras(mBundle);
							mContext.startActivity(intent);
							
						} catch (Exception ex) {
							ex.printStackTrace();
						}
					}
				}
			});
		}
		
		if(activity.action_type != 0 && activity.action_type != 5)
			activityItem.mAppIcon.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					try  
					{
						// Start application activity
						 Intent intent = new Intent(); 
						 intent.setClass(mContext, ApplicationActivity.class);
	
						 Bundle mBundle = new Bundle(); 
						 mBundle.putString("package_name", activity.app_pckName);
						 mBundle.putInt("n", 20);
						 String uid = LocalStore.getLocaleStoreInstance(mContext).getUid();
						 mBundle.putString("uid", uid);
						 mBundle.putBoolean("installed_not_addicted", true);
						 
						 PackageManager manager = mContext.getPackageManager();
						 try 
						 {
							 PackageInfo info = manager.getPackageInfo(activity.app_pckName, 0);
							 if (info != null)
							 {
								 mBundle.putBoolean("user_app_flag", true);
							 }
							 else
							 {
								 mBundle.putBoolean("user_app_flag", false);
							 }

						 } catch (Exception e) {
							 mBundle.putBoolean("user_app_flag", false);
						 }
						 
						 /* Send the icon */
						mBundle.putString("app_icon", activity.app_icon);
						if (activity.app_icon_drawable != null) 
						{
							try 
							{
								Bitmap bitmap = ((BitmapDrawable) activity.app_icon_drawable).getBitmap();
								ByteArrayOutputStream stream = new ByteArrayOutputStream();
	
								bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
								byte[] bitmapdata = stream.toByteArray();
								mBundle.putByteArray("app_icon_array", bitmapdata);
								
							} catch (Exception ex) {
								ex.printStackTrace();
								mBundle.putByteArray("app_icon_array", null);
							}
	
						} else {
							mBundle.putByteArray("app_icon_array", null);
						}
						mBundle.putString("app_name", activity.app_name);
						intent.putExtras(mBundle);
						mContext.startActivity(intent);
						
					} catch(Exception ex) {
						ex.printStackTrace();
					}
				}
			});
		else
			activityItem.mAppIcon.setOnClickListener(null);
		
		return convertView;
	}

	class ActivityItem 
	{
		ImageView mUserIcon;
		ImageView weiboIcon;
		TextView mUserName;
		TextView mActivityDetails;
		TextView mActivityDate;
		ImageView mAppIcon;
	}
}
